import { isBlank, isValidNumber, timeStampToDate } from '@/utils/utilsNew';
import { positiveNumberRules, numberRules, forceHanlder, requiredRule } from './rules';
import { validHouseCode } from '@/utils/validate';

// 房源详情配置数据
export const configHouse = [
  {
    title: '小区',
    required: true,
    isInput: true,
    key: 'resblockName',
    // contents: [{ key: 'resblockName', type: 'input', width: '200px', placeholder: '输入小区', readonly: true }],
    width: '200px',
    disabled: true,
  },
  {
    title: '区域',
    required: false,
    isMultiple: true,
    contents: [
      { key: 'area', type: 'input', width: '100px', placeholder: '请选择区域', readonly: true },
      { key: 'addr', type: 'input', width: '100px', placeholder: '请输入地址', readonly: true },
    ],
    width: '200px',
  },
  {
    title: '售价',
    required: true,
    key: 'totalPrices',
    unit: '万元/套',
    tips: '注：售价不能小于2万元/套',
    width: '200px',
    isInput: true,
    rules: [
      requiredRule,
      {
        validator: (rule, value, callback) => {
          if (!isValidNumber(value)) {
            callback(new Error('必须为数字'));
          } else if (value < 2) {
            callback(new Error('不能小于2万元/套'));
          } else if (isBlank(value)) {
            callback(new Error('不能有空格'));
          }
        },
        trigger: 'blur',
      },
    ],
    checkValue: (v) => v > 2,
    force: true,
  },
  {
    title: '户型',
    required: true,
    isMultiple: true,
    contents: [
      { key: 'room', type: 'input', width: '100px', unit: '室', base: true, rules: [requiredRule, numberRules] },
      { key: 'hall', type: 'input', width: '100px', unit: '厅', base: true, rules: [requiredRule, numberRules] },
      { key: 'toilet', type: 'input', width: '100px', unit: '卫', base: true, rules: [requiredRule, numberRules] },
      { key: 'cookroom', type: 'input', width: '100px', unit: '厨', rules: [requiredRule, numberRules] },
      { key: 'balcony', type: 'input', width: '100px', unit: '阳台', rules: [requiredRule, numberRules] },
    ],
    checkValue: (v) => isValidNumber(v),
    force: true,
  },
  {
    title: '建筑面积',
    required: true,
    isInput: true,
    key: 'buildSize',
    width: '200px',
    unit: '平方米',
    rules: [requiredRule, positiveNumberRules],
    checkValue: (v) => v > 0,
    force: true,
  },

  {
    title: '房屋类型',
    required: true,
    key: 'buildType',
    options: [
      '住宅',
      '复式住宅',
      '公寓',
      '别墅',
      '商铺',
      '写字楼',
      '平房',
      '新里洋房',
      '老公房',
      '动迁房',
      '经济适用房',
      '其他',
    ],
    isRadio: true,
  },
  // {
  //   "title": "产权性质",
  //   "required": false,
  //   "key": "propetyType",
  //   "options": [
  //     "产权",
  //     "合同",
  //     "协议",
  //     "新证",
  //     "老证",
  //     "公证书"
  //   ],
  //   "isRadio": true
  // },

  {
    title: '产权类型',
    required: true,
    key: 'propertyType',
    options: [
      '商品房住宅',
      '商住两用',
      '保障性住房',
      '公房',
      '动迁配套房',
      '个人产权',
      '单位产权',
      '经济适用房',
      '限价房',
      '其他',
    ],
    isRadio: true,
  },

  {
    title: '朝向',
    required: true,
    key: 'direction',
    options: ['东', '南', '西', '北', '西南', '西北', '东南', '东北', '南北', '东西'],
    isRadio: true,
  },
  {
    title: '装修',
    required: true,
    key: 'decorateType',
    options: ['毛坯', '简装', '精装', '豪装'],
    isRadio: true,
  },
  {
    title: '产权期限',
    key: 'propetyTime',
    required: true,
    options: [70, 50, 40],
    format: (v) => v + '年产权',
    isRadio: true,
  },
  {
    title: '房屋性质',
    key: 'houNature',
    required: true,
    options: ['平层', '开间', '复式', '跃层', '错层'],
    isRadio: true,
  },
  {
    title: '建筑形式',
    required: true,
    key: 'useType',
    options: ['砖楼', '板楼', '塔楼', '砖混', '平房', '钢混', '框架'],
    isRadio: true,
  },

  {
    title: '所在楼层',
    required: true,
    isInput: true,
    key: 'floor',
    width: '200px',
    unit: '楼',
    tips: '注：若是地下室，请填负数',
    rules: [
      requiredRule,
      {
        validator: (rule, value, callback) => {
          if (!isValidNumber(value)) {
            callback(new Error('必须为数字'));
          } else if (value > 99) {
            callback(new Error('所在楼层不能大于99'));
          } else if (isBlank(value)) {
            callback(new Error('不能有空格'));
          }
        },
        trigger: 'blur',
      },
    ],
    checkValue: (v) => isValidNumber(v) && v < 99,
  },
  {
    title: '总楼层',
    required: true,
    isInput: true,
    key: 'totalFloor',
    unit: '层',
    width: '200px',
    rules: [
      requiredRule,
      {
        validator: (rule, value, callback) => {
          if (!(value >= 1 && value <= 99)) {
            callback(new Error('总楼层不能小于1且不能大于99'));
          } else if (isBlank(value)) {
            callback(new Error('不能有空格'));
          }
        },
        trigger: 'blur',
      },
    ],
    checkValue: (v) => isValidNumber(v) && v < 99,
  },
  {
    title: '使用面积',
    required: false,
    isInput: true,
    key: 'useSize',
    width: '200px',
    unit: '平方米',
    rules: [numberRules],
    checkValue: (v) => isValidNumber(v),
  },
  {
    title: '佣金比例',
    required: true,
    key: 'commissionRate',
    tips: '注：请尽量填写佣金最高比例（佣金比例范围0.5%-5%，且佣金只能保留一位小数）',
    unit: '%',
    isInput: true,
    width: '200px',
    rules: [
      requiredRule,
      {
        validator: (rule, value, callback) => {
          if (!isValidNumber(value)) {
            callback(new Error('必须为数字'));
          } else if (value > 5 || value < 0.5) {
            callback(new Error('佣金比例范围0.5%-5%'));
          } else if (isBlank(value)) {
            callback(new Error('不能有空格'));
          } else if (value.indexOf('.') != -1 && value.slice(value.indexOf('.') + 1).length > 1) {
            callback(new Error('佣金比例只能保留一位小数'));
          }
        },
        trigger: 'blur',
      },
    ],
    button: {
      buttonText: '设置默认佣金比例',
    },
    checkValue: (v) =>
      isValidNumber(v) &&
      v >= 0.5 &&
      v <= 5 &&
      (String(v).indexOf('.') == -1 || String(v).slice(String(v).indexOf('.') + 1).length <= 1),
  },
  {
    title: '建造年代',
    required: true,
    isInput: true,
    key: 'timeBuild',
    width: '200px',
    tips: '注：建造年代必须大于或等于1900年',
    unit: '年',
    rules: [
      requiredRule,
      {
        validator: (rule, value, callback) => {
          if (!isValidNumber(value)) {
            callback(new Error('必须为数字'));
          } else if (value < 1900) {
            callback(new Error('建造年代必须大于或等于1900年'));
          } else if (isBlank(value)) {
            callback(new Error('不能有空格'));
          }
        },
        trigger: 'blur',
      },
    ],
    checkValue: (v) => isValidNumber(v) && v >= 1900,
  },
  {
    title: '房源年限',
    required: true,
    key: 'houYears',
    options: ['满五年', '满二年', '不满二年'],
    isRadio: true,
  },
  {
    title: '唯一住房',
    required: true,
    key: 'onlyHou',
    options: [0, 1],
    format: (v) => (v == '1' ? '否' : '是'),
    isRadio: true,
  },
  {
    title: '是否配有电梯',
    required: true,
    key: 'elevator',
    options: [0, 1],
    format: (v) => (v == '1' ? '否' : '是'),
    isRadio: true,
  },
  {
    title: '配套设施',
    required: false,
    key: 'matchingFacilitiesList',
    options: [
      '水',
      '电',
      '煤气',
      '暖气',
      '有线电视',
      '宽带',
      '电梯',
      '车位',
      '储藏室',
      '花园',
      '露台',
      '自带权属车位',
      '天井',
      '地下城',
      '飘窗',
    ],
    isCheckbox: true,
  },
];
configHouse.forEach((item) => {
  if (item.required && !item.rules) {
    item.rules = [requiredRule];
  }
});
const forceConfigs = configHouse.filter((v) => v.force);

export const forceHouseInfoHanlder = forceHanlder.bind(null, forceConfigs);

export const portHouNumConfig = {
  rules: [
    requiredRule,
    {
      validator: (rule, value, callback) => {
        if (!isValidNumber(value)) {
          callback(new Error('必须为数字'));
        } else if (isBlank(value)) {
          callback(new Error('不能有空格'));
        }
      },
      trigger: 'blur',
    },
  ],
  checkValue: (v) => isValidNumber(v),
  rules1: [
    requiredRule,
    {
      validator: (rule, value, callback) => {
        if (!validHouseCode(value)) {
          callback(new Error('必须为数字、字母、中横线、下横线'));
        } else if (isBlank(value)) {
          callback(new Error('不能有空格'));
        }
      },
      trigger: 'blur',
    },
  ],
  checkValue1: (v) => validHouseCode(v),
};

/** 户室号 */
export const hallRoomNumber = [
  {
    title: '栋/弄/胡同',
    key: 'portBuild',
    unit: '栋',
  },
  {
    title: '单元/号',
    key: 'portUnit',
    unit: '单元',
  },
  {
    title: '门牌',
    key: 'portDoorNum',
    unit: '号',
  },
];

// 关于字数的校验
const tipsFormat = function() {
  let str = `注：${this.title}限${this.minlength}~${this.maxlength}个汉字`;
  return this.required ? str + `，低于${this.minlength}个汉字，会导致房源质量评分为“低”` : str;
};

// 校验是否含有连续的5位数字（不能包含5位连续的数字）
const chineseReg = /[\u4E00-\u9FA5]/g;
const rule = function(str = '') {
  if (/[0-9]{5}/g.test(str)) return `${this.title}不能包含连续的5位数字`;
  let arr = str.match(chineseReg);
  if (!arr || arr.length < this.minlength) return `${this.title}不足${this.minlength}个汉字`;
  return '';
};

/** 房源介绍 */
export const houseRecommend = [
  {
    title: '房源标题',
    key: 'title',
    required: true,
    minlength: 10,
    maxlength: 30,
    rows: 2,
    tipsFormat,
    rule,
  },
  {
    title: '核心卖点',
    key: 'sellingPoint',
    required: true,
    minlength: 20,
    maxlength: 300,
    rows: 5,
    tipsFormat,
    rule,
  },
  {
    title: '业主心态',
    key: 'ownerPsychology',
    required: true,
    minlength: 20,
    maxlength: 300,
    rows: 5,
    tipsFormat,
    rule,
    // defaultValue: "房东因工作变动诚心急售此房，房东为人和善，随时配合看房",
  },
  {
    title: '服务介绍',
    key: 'serviceIntroduction',
    required: true,
    minlength: 20,
    maxlength: 300,
    rows: 5,
    tipsFormat,
    rule,
    // defaultValue: "本人从事房产多年，真诚的服务，从选房到交易完成全程为您服务、护航",
  },
  {
    title: '小区配套',
    key: 'surroundMating',
    required: false,
    minlength: 20,
    maxlength: 300,
    rows: 5,
    tipsFormat,
    rule,
  },
  {
    title: '税费信息',
    key: 'service',
    required: false,
    minlength: 20,
    maxlength: 300,
    rows: 5,
    tipsFormat,
    rule,
  },
];

/** 房源图片 */
export const houseImages = [
  {
    title: '户型图',
    tagCode: 'huxingtu',
    tagName: '户型图',
    tips: '注：至少上传1张，最多10张；图片宽度，高度需≥300像素。',
    key: 'houseTypeList',
    minlength: 1,
    maxlength: 10,
    minWidth: 300,
    minHeight: 300,
    draggable: false,
    options: [
      { text: '上传图片', option: 'upload' },
      { text: '在线户型图', option: 'add' },
    ],
  },
  {
    title: '室内照片',
    tagCode: '',
    tagName: '',
    tips: '注：至少上传3张，最多30张；图片宽度需≥600像素。',
    key: 'indoorList',
    minlength: 3,
    maxlength: 30,
    minWidth: 600,
    minHeight: 0,
    draggable: true,
    options: [
      { text: '上传图片', option: 'upload' },
      { text: '补充室内图', option: 'add' },
      { text: '去除实勘牌', option: 'watermark'},
    ],
  },
  {
    title: '小区照片',
    tagCode: 'xiaoqu',
    tagName: '小区',
    tips: '注：最多10张；图片宽度需≥600像素。',
    key: 'circumList',
    minlength: 0,
    maxlength: 10,
    minWidth: 600,
    minHeight: 0,
    draggable: false,
    options: [{ text: '上传图片', option: 'upload' }],
  },
];

//（二手房视频和租房视频：一室户不得小于30秒，二室户不得小于40秒，三室户不得小于50秒，四室户以上不得小于60秒的视频；商业地产视频不得小于15秒）；
export const videoTimeList = {
  '1': 31,
  '2': 41,
  '3': 51,
  '4': 61,
};
